# encoding: utf-8

class UserApi < Grape::API
  helpers ::ToolKit
  prefix :users

  get '/:id' do
    User.find(params[:id])
  end

  post 'reg' do
    User.reg params[:username], params[:password]
  end

  post '/login' do
    user = User.login params[:username], params[:password]
    session['current_user'] = user.safe_attributes
  end

  post '/logout' do
    session['current_user']=nil
  end

  get '/current' do
    @current_user.safe_attributes if @current_user
  end

  put '/current' do
    require_authorized!
    @current_user.update! params[:name], params[:email], params[:password], params[:gender], params[:old_password]
  end
end